//
//  CommentViewController.m
//  Four_Fit_New
//
//  Created by lanou on 16/7/14.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "CommentViewController.h"
#import "Community.h"
#import "CommunityCell.h"
#import "comment.h"
#import "CommentCell.h"

@interface CommentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSapce;
@property (weak, nonatomic) IBOutlet UIButton *button;
/** <#注释#> */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** <#注释#> */
@property (nonatomic, strong) NSMutableArray *array;
/** <#注释#> */
@property (nonatomic, strong) comment *comment;
@end

static NSString *const cellID = @"Comment_cell";
@implementation CommentViewController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil] forCellReuseIdentifier:cellID];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
 
    [self loadComment];
    
}

- (void)keyboardWillChangeFrame:(NSNotification *)note {
    // 键盘显示\隐藏完毕的frame
    CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 修改底部约束
    self.bottomSapce.constant = kHeight - frame.origin.y;
    // 动画时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // 动画 及时刷新
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
    
}

- (void)setupHeader {
    self.navigationItem.title = self.community.username;
    
    
    UIView *header = [[UIView alloc] init];
    self.comment = self.array[0];
    
    CommunityCell *cell = [CommunityCell cell];
    cell.size = CGSizeMake(kWidth, self.comment.headerHeight);
    cell.comment = self.comment;
    
    [header addSubview:cell];

    header.height = cell.comment.headerHeight + 10;

    self.tableView.tableHeaderView = header;
}

- (void)loadComment {
    
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"noteid"] = self.community.ID;
    parame[@"tokenid"] = @"tokenid_undefined";
    parame[@"userid"] = @"5784da4c4925c70c581758a9";
    parame[@"time"] = @"1468392215182";

    
    [[QZRequestManager sharedManager] fetchDataWithUrl:@"http://ws.sythealth.com/ws/fit/newforum/note/getnoteandcommbyid" parameters:parame method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"%@",dic[@"data"]);

        for (NSDictionary *commentDic in dic[@"data"][@"items"]) {
            comment *cot = [[comment alloc] init];
            [cot setValuesForKeysWithDictionary:commentDic];
            [self.dataArray addObject:cot];
        }
        
        comment *com = [[comment alloc] init];
        [com setValuesForKeysWithDictionary:dic[@"data"]];
        [self.array addObject:com];
        
    } updateUI:^{
        [self setupHeader];
        [self.tableView reloadData];
    }];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell.comment = self.dataArray[indexPath.row];
    
    
    return cell;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 150;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
